package bb.lanxing.activity.team;

import static bb.lanxing.common.config.Constants.REQUEST_PREFIX;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.activity.base.WebActivity;
import bb.lanxing.adapter.LessonAdapter;
import bb.lanxing.model.data.LessonData;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LessonActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private LessonAdapter lessonAdapter;
    private List<LessonData> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson);

      setTitle(getString(R.string.discover_4));
      setupActionBar(true);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        lessonAdapter = new LessonAdapter();
        lessonAdapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(this, WebActivity.class);
            intent.putExtra("web_url", list.get(position).linkUrl);
            startActivity(intent);
        });
        recyclerView.setAdapter(lessonAdapter);

        loadData();
    }

    private void loadData() {
        Observable.create(new NetSubscribe(MyHttpClient.requestGetDiscoveryHelp(1, 10, 1)))
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(String s) {
                        list = new ArrayList<>();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int total = jsonObject.getInt("total");
                            JSONArray jsonArray = jsonObject.optJSONArray("rows");
                            if (null != jsonArray) {
                                for (int i = 0; i < total; i++) {
                                    JSONObject itemJsonObject = jsonArray.getJSONObject(i);
                                    String title = itemJsonObject.getString("title");
                                    String remark = itemJsonObject.optString("remark");
                                    String imageUrl = itemJsonObject.getString("imageUrl");
                                    String linkUrl = itemJsonObject.getString("linkUrl");
                                    String fullImageUrl = MyHttpClient.DOMAIN + REQUEST_PREFIX + imageUrl;
                                    LessonData lessonData = new LessonData();
                                    lessonData.title = title;
                                    lessonData.remark = remark;
                                    lessonData.linkUrl = linkUrl;
                                    lessonData.fullImageUrl = fullImageUrl;
                                    list.add(lessonData);
                                }
                                lessonAdapter.updateData(list);
                            }
                        } catch (JSONException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
//        isDataLoaded = true;
    }
}